﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

using System.Data.Entity;


namespace InvoiceProject.Models
{
    public class Invoice : BaseModel, IValidatableObject
    {

        [Required]
        public Customer Customer { get; set; }

        [Required]
        public Business Business { get; set; }

        [Required]
        [Display(Name = "Total Including GST")]
        [Column(TypeName = "money")]
        public decimal TotalIncTax { get; set; }

        [Required]
        [Display(Name = "Total GST")]
        [Column(TypeName = "money")]
        public decimal TotalTax { get; set; }

        [Required]
        public DateTime InvoiceDate { get; set; }

        public List<InvoiceItem> InvoiceItems { get; set; }




        public new IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (this.TotalIncTax >0 && this.TotalTax ==0 && Business.LookupTaxCountry.TaxRate != 0)
                yield return new ValidationResult("Total Tax can not be Zero when TotalInclTax > 0 and Business Tax Rate is not Zero ");


            if (this.TotalTax > 0 && this.TotalIncTax == 0)
                yield return new ValidationResult("TotalInclTax can not be Zero when TotalIncTax");

            ////decimal expectedTotalIncTax = 0;
            ////decimal expectedTax = 0; 
            ////foreach (var invoiceItem in this.InvoiceItems)
            ////{
            ////    var total = invoiceItem.Quantity * invoiceItem.Rate;
            ////    expectedTax += total * Business.LookupTaxCountry.TaxRate;
            ////    expectedTotalIncTax += total + invoiceItem.Tax;

            ////}

            ////if (this.TotalIncTax != expectedTotalIncTax)
            ////    yield return new ValidationResult("Invoice Total Incl Tax does not equal expected Total, based on associated invoiceItems");

            ////if (this.TotalTax != expectedTax)
            ////    yield return new ValidationResult("Invoice Tax does not equal expected Total, based on associated invoiceItems");
 
            //if ((this.TotalIncTax) == 0)
            //    yield return new ValidationResult("TotalIncTax can not be zero");

            //if ((this.TotalTax) == 0)
            //    yield return new ValidationResult("Total Tax has not been calculated");


        }



        public void Calculate()
        {

            this.TotalIncTax = 0;
            this.TotalTax = 0;
            
            foreach (var invoiceItem in this.InvoiceItems)
            {
                var total = invoiceItem.Quantity * invoiceItem.Rate;
                invoiceItem.Tax =  total * Business.LookupTaxCountry.TaxRate;
                invoiceItem.SubTotal = total + invoiceItem.Tax;

                TotalTax += invoiceItem.Tax;
                TotalIncTax += invoiceItem.SubTotal;
            }



        }
    }



    public class InvoiceItem:BaseModel
    {
    
        public string Description { get; set; }


        [Display(Name = "Quantiy")]
        [Column(TypeName = "money")]
        public decimal Quantity { get; set; }

        [Display(Name = "Rate (Exl GST)")]
        [Column(TypeName = "money")]
        public decimal Rate { get; set; }


        [Display(Name = "GST")]
        [Column(TypeName = "money")]
        public decimal Tax { get; set; }


        [Display(Name = "Sub Total")]
        [Column(TypeName = "money")]
        public decimal SubTotal { get; set; }

    }


      

}